<template>
  <view>
   <view class="cart-container" v-if="cart.length !== 0">
     <!-- 收货地址组件 -->
     <my-adress></my-adress>
     <!-- 商品列表标题区域 -->
     <view class="item_list_title">
       <uni-icons type="shop-filled" size="17"></uni-icons>
       <text>购物车</text>
     </view>
     
     <view class="cart_list">
       <!-- 商品列表区域 -->
       <uni-swipe-action>
         <uni-swipe-action-item :right-options="options" @click="bindClick" v-for="(item, i) in cart" :key="i">
           <my-goods :showRadio="true" :item="item" @radion-cahnge="radioChangeHandler" :showNumberBox="true" @num-change="numberChange"></my-goods>
         </uni-swipe-action-item>
       </uni-swipe-action>
     </view>
     <!-- 使用自定义结算组件 -->
     <my-settle></my-settle>
   </view>
   
   <view class="empty-container" v-else>
     <image src="../../static/cart_empty@2x.png" class="empty-image"></image>
     <text class="tip-text">空空如也～</text>
   </view>
  </view>
</template>

<script>
  import badgeMix from '../../mixins/mixins.js'
  import {
    mapState
  } from 'vuex'
  import myGoods from '@/components/my-goods/my-goods'
  export default {
    components: {
      'my-goods': myGoods
    },
    mixins: [badgeMix],
    data() {
      return {
        options: [{
          text: '删除',
          style: {
            backgroundColor: '#C00000'
          }
        }]
      };
    },
    computed: {
      ...mapState('m_cart', ['cart'])
    },
    methods: {
      radioChangeHandler(e) {
        // console.log(e)
        // 更新购物车
        const good = this.cart.find(item => item.goods_id === e.goods_id)
        good.goods_state = e.goods_state
        this.$store.commit('m_cart/saveToStorage')
      },
      numberChange(e) {
        // console.log(e)
        const result = this.cart.find(item => item.goods_id === e.goods_id)
        result.goods_count = e.goods_count
        this.$store.commit('m_cart/saveToStorage')
      },
      bindClick(e) {
        console.log(e)
        this.$store.commit('m_cart/removeCartList',e.index)
      }
    }
  }
</script>

<style lang="scss">
  .item_list_title {
    height: 40px;
    display: flex;
    align-items: center;
    padding-left: 5px;
    border-bottom: 1px solid #efefef;
   uni-icons {
      margin-right: 10px;
   }
    text {
      font-size: 13px;
      
    }
  }
  .cart_list {
    padding-bottom: 50px;
  }
  .empty-container {
    position: absolute;
    top: 50%;
    left: 50%;
    text-align: center;
    transform: translate3d(-50%,-50%,0);
    .empty-image {
      width: 90px;
      height: 90px;
      display: block;
      margin-bottom: 15px;
    }
    .tip-text {
      font-size: 12px;
      color: gray;
      margin-left: 8px;
    }
    
  }
  
</style>
